package com.mtu.parser;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;

public class TableParserNet3 
{
	
	public static void main(String[] args) throws IOException  {
		
		int i=0,j=0;
		String url="http://www.uptu.ac.in/results/EVEN_SEMESTER_10_11/bte6_10_11.asp?rollno=0806410005";

		Document doc = Jsoup.connect(url).get(); 
		Elements table = doc.select("table");
    	int size=table.size();
    	Element [] arrOfTables = new Element[size];
    	
    	for (Element src : table) {
    		arrOfTables[i]=src;   //Data is stored from arrOfTables[1] onwards.  
    		i++;		
        }
    	i=0;
        //System.out.println(arrOfTables[3].text());
    	

    	/* This section of code scraps the personal information
    	 * The personal information is stored in the arrOfTables[1]
    	 * Then student's name, father's name etc are extracted*/
    	
    	Elements firstTable=arrOfTables[1].select("td");
    	int firstTableSize=firstTable.size();
    	String[] personalInfo= new String[firstTableSize];
    	for (Element src : firstTable) {
    		personalInfo[j]=src.text().trim();   //Data is stored from tableData[0] onwards.  
    		j++;		
        }
    	j=0;
    	
    	String name = null,fatherName = null,rollNo = null,status = null,branch = null,college = null;
    	
    	for(i=0;i<firstTableSize;i=i+2)
    	{
    		if (personalInfo[i].equalsIgnoreCase("Name:")) {
    			name=personalInfo[i+1];
    			continue;
    		}
    		if(personalInfo[i].equalsIgnoreCase("Fathers Name:")) {
    			fatherName=personalInfo[i+1];
    			continue;
    		}
    		if(personalInfo[i].equalsIgnoreCase("Roll No:")) {
    			rollNo=personalInfo[i+1];
    			continue;
    		}
    		if(personalInfo[i].equalsIgnoreCase("Status:")) {
    			status=personalInfo[i+1];
    			continue;
    		}
    		if(personalInfo[i].equalsIgnoreCase("Course/Branch:")) {
    			branch=personalInfo[i+1];
    			continue;
    		}
    		if(personalInfo[i].equalsIgnoreCase("Institute Name")) {
    			college=personalInfo[i+1];
    			continue;
    		}
    	}
    	
    	System.out.println("Name= "+name);	
    	System.out.println("Father's Name= "+fatherName);
    	System.out.println("Roll no= "+rollNo); 	
    	System.out.println("Status= "+status);
    	System.out.println("Branch= "+branch);
    	System.out.println("College= "+college);
    	
        
        /* This section of code stores the marks of the 5th semester in the array named oddMarksInfo[]
         * This code also takes care that the empty entries in the table; eg xx are not entered in the arrays
         * The gp marks are also stored in the externalMarks[] only and not in the internalMarks[]
         * Number of subjects are taken as 11  
         */
    	
    	System.out.println();
    	System.out.println();
    	System.out.println("MARKS DETAILS ODD SEMESTER");
    	
    	Elements thirdTable=arrOfTables[3].select("td");
    	int thirdTableSize=thirdTable.size();
    	String[] oddMarksInfo=new String[thirdTableSize];
    	for (Element src : thirdTable) {
    		oddMarksInfo[j]=src.text().trim();   //Data is stored from tableData[0] onwards.  
    		j++;		
        }
    	j=0;
    	
    	int oddNoOfSubjects=11;
    	String[] oddSubjectName= new String[oddNoOfSubjects];
    	int[] oddExternalMarks = new int[oddNoOfSubjects];
    	int[] oddInternalMarks = new int[oddNoOfSubjects];
    	int[] oddCredit = new int[oddNoOfSubjects];
    	int oddPositionCounter=0;
    	
    	for(i=4;i<thirdTableSize;i=i+4) {
    		
    		if(oddMarksInfo[i+1].equalsIgnoreCase("xx")) {
    			continue;
    		}
    		
    		if(oddMarksInfo[i].substring(0,2).equalsIgnoreCase("gp")) {
    			oddSubjectName[oddPositionCounter]=oddMarksInfo[i];
    			oddExternalMarks[oddPositionCounter]=Integer.parseInt(oddMarksInfo[i+1]);
    			oddInternalMarks[oddPositionCounter]=0;
    			oddPositionCounter++;
    			continue;
    		}
    		
    		oddSubjectName[oddPositionCounter]=oddMarksInfo[i];
    		oddExternalMarks[oddPositionCounter]=Integer.parseInt(oddMarksInfo[i+1]);
    		oddInternalMarks[oddPositionCounter]=Integer.parseInt(oddMarksInfo[i+2]);
    		oddCredit[oddPositionCounter]=Integer.parseInt(oddMarksInfo[i+3]);
    		
    		oddPositionCounter++;
    	}
    	
    	System.out.print("Subject Code\t External Marks\t Sessional Marks\t Credit");
    	System.out.println();
    	
    	for(i=0;i<oddNoOfSubjects;i++) {
    		System.out.print(oddSubjectName[i]+"\t");
        	System.out.print(oddExternalMarks[i]+"\t");
        	System.out.print(oddInternalMarks[i]+"\t");
        	System.out.print(oddCredit[i]+"\t");
        	System.out.println();
    	}
    	
    	
    	/* This section of code stores the marks of the 6th semester in the array named evenMarksInfo[]
         * This code also takes care that the empty entries in the table; eg --- are not entered in the arrays
         * The gp marks are also stored in the externalMarks[] only and not in the internalMarks[]
         * Number of subjects are taken as 11  
         */
    	
    	System.out.println();
    	System.out.println();
    	System.out.println("MARKS DETAILS EVEN SEMESTER");
    	
    	Elements fourthTable=arrOfTables[4].select("td");
    	int fourthTableSize=fourthTable.size();
    	String[] evenMarksInfo=new String[fourthTableSize];
    	for (Element src : fourthTable)  {
    		evenMarksInfo[j]=src.text().trim();   //Data is stored from tableData[0] onwards.  
    		j++;		
        }
    	
    	int evenNoOfSubjects=11;
    	String[] evenSubjectName= new String[evenNoOfSubjects];
    	int[] evenExternalMarks = new int[evenNoOfSubjects];
    	int[] evenInternalMarks = new int[evenNoOfSubjects];
    	int[] evenCredit = new int[evenNoOfSubjects];
    	int evenPositionCounter=0;
    	
    	for(i=4;i<fourthTableSize;i=i+4) {
    		
    		if(evenMarksInfo[i+1].equalsIgnoreCase("---")) {
    			evenSubjectName[evenPositionCounter]=evenMarksInfo[i];
    			evenExternalMarks[evenPositionCounter]=0;
    			evenInternalMarks[evenPositionCounter]=Integer.parseInt(evenMarksInfo[i+2]);
    			evenPositionCounter++;   			
    			continue;
    		}
    		
    		if(evenMarksInfo[i].substring(0,2).equalsIgnoreCase("gp")) {
    			evenSubjectName[evenPositionCounter]=evenMarksInfo[i];
    			evenExternalMarks[evenPositionCounter]=Integer.parseInt(evenMarksInfo[i+1]);
    			evenInternalMarks[evenPositionCounter]=0;
    			evenPositionCounter++;
    			continue;
    		}
    		
    		evenSubjectName[evenPositionCounter]=evenMarksInfo[i];
    		evenExternalMarks[evenPositionCounter]=Integer.parseInt(evenMarksInfo[i+1]);
    		evenInternalMarks[evenPositionCounter]=Integer.parseInt(evenMarksInfo[i+2]);
    		evenCredit[evenPositionCounter]=Integer.parseInt(evenMarksInfo[i+3]);
    		
    		evenPositionCounter++;
    	}
    	
    	System.out.print("Subject Code\t External Marks\t Sessional Marks\t Credit");
    	System.out.println();
    	
    	for(i=0;i<evenNoOfSubjects;i++) {
    		System.out.print(evenSubjectName[i]+"\t");
        	System.out.print(evenExternalMarks[i]+"\t");
        	System.out.print(evenInternalMarks[i]+"\t");
        	System.out.print(evenCredit[i]+"\t");
        	System.out.println();
    	}
	}
	
}
